package com.eurasia.assignment.service;

import java.io.IOException;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.eurasia.assignment.domain.ProjectAssignment;
import com.eurasia.assignment.domain.Submission;
import com.eurasia.assignment.infrastructure.entity.FileEntity;
import com.eurasia.assignment.infrastructure.entity.ProjectAssignmentEntity;
import com.eurasia.assignment.infrastructure.entity.SubmissionEntity;
import com.eurasia.assignment.infrastructure.repository.ProjectAssignmentRepository;
import com.eurasia.assignment.infrastructure.repository.SubmissionRepository;
import com.eurasia.assignment.service.port.in.CreateSubmissionUseCase;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

@Service
@RequiredArgsConstructor
@Slf4j
public class CreateSubmissionService implements CreateSubmissionUseCase {
    private final ProjectAssignmentRepository projectAssignmentRepository;
    private final SubmissionRepository submissionRepository;
    private final FileService fileService;
    
    @Override
    @Transactional
    public void createSubmission(CreateSubmissionCommand command) {
        // 1. 获取 根据projectId 获取 projectAssignment，判断其是否存在
        ProjectAssignmentEntity assignmentEntity = projectAssignmentRepository.findByProjectId(command.projectId())
                .orElseThrow(() -> new ProjectAssignmentNotFoundException(command.projectId()));
        
        ProjectAssignment assignment = assignmentEntity.toDomain();
        
        try {
            // 2. 保存文件
            FileEntity fileEntity = fileService.saveFile(command.file());
            
            // 3. 通过 projectAssignment.addSubmission 创建 submission
            Submission submission = assignment.addSubmission(
                    command.description(),
                    command.type(),
                    command.submitterId(),
                    fileEntity
            );
            
            // 4. 保存 submission
            submissionRepository.save(SubmissionEntity.fromDomain(submission));
            
            log.info("成功创建提交 ID: {}, 项目ID: {}", submission.getId(), command.projectId());
        } catch (IOException e) {
            log.error("文件保存失败", e);
            throw new FileUploadException("文件保存失败: " + e.getMessage());
        } catch (Exception e) {
            log.error("创建提交失败", e);
            throw new SubmissionCreationException("创建提交失败: " + e.getMessage());
        }
    }
    
    public static class ProjectAssignmentNotFoundException extends RuntimeException {
        public ProjectAssignmentNotFoundException(Long projectId) {
            super("项目分配不存在: " + projectId);
        }
    }
    
    public static class FileUploadException extends RuntimeException {
        public FileUploadException(String message) {
            super(message);
        }
    }
    
    public static class SubmissionCreationException extends RuntimeException {
        public SubmissionCreationException(String message) {
            super(message);
        }
    }
}
